﻿@model IEnumerable<Commission.ViewModels.TransactionHistorySum>
@using MyResources = Commission.Resources.AppResource
@using Microsoft.AspNet.Identity
@using Commission.Models;

@{
    ViewBag.Title = @MyResources.LabelAccount;
}

<h2>@MyResources.LinkAccountSumList</h2>

@section MyJavascript {
    <script type="text/javascript">
    $(document).ready(function () {
        $('#UserName').autocomplete(
        {
            source: '@Url.Action("CustomerSearch", "Customer")'
        }
        );
    })
    </script>
}

@section Sortable {
    @Scripts.Render("~/bundles/sortable")
}

@using (Html.BeginForm("SumIndex", "Transaction", FormMethod.Get))
{
    <p>
        @MyResources.LabelDateFrom:
        @Html.JQueryUI().Datepicker("dateFrom", ViewBag.DateFrom,
             new { @class = "form-control-date searchtext", @style = "width:88px" })

        @MyResources.LabelDateTo:
        @Html.JQueryUI().Datepicker("dateTo", ViewBag.DateFrom,
             new { @class = "form-control-date searchtext", @style = "width:88px" })

        @MyResources.LabelCustomer : @Html.TextBox("UserName", ViewBag.CurrentFilter as string,
            new { @class = "form-control searchtext" })
      
        <input type="submit" value="@MyResources.LabelSearch" style="height:28px" />
    </p>
}

<table class="table hoverTable sortable">
    <tr>
        <th class="text-center" style="width:75px">
            @MyResources.LabelDate
        </th>

        @if (User.IsInRole("Admin"))
        {
            <th class="text-left" style="width:60px;">
                @MyResources.LabelUserName
            </th>

            <th class="text-center" style="width:120px;">
                @MyResources.LabelName
            </th>
        }

        <th class="text-center" style="width:56px;">
            @MyResources.LabelDirectAmount
        </th>

        <th class="text-center" style="width:56px;">
            @MyResources.LabelRollupAmount
        </th>

        <th class="text-center" style="width:56px;">
            @MyResources.LabelPairingAmount
        </th>

        <th class="text-center" style="width:56px;">
            @MyResources.LabelOnlineAmount
        </th>

        <th class="text-center" style="width:56px;">
            @MyResources.LabelMatchingAmount
        </th>

        <th class="text-center" style="width:78px;">
            @MyResources.LabelUsedAmount
        </th>

        <th class="text-center" style="width:90px;">
            @MyResources.LabelTotal
        </th>

        <th class="text-center" style="width:70px;">
            @MyResources.LabelRemain
        </th>

        <th class="text-left" style="width:60px;"></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td class="text-center">
                @Html.Label(item.AccountDate.ToShortDateString(), new { @class = "LabelNormal" })
            </td>

            @if (User.IsInRole("Admin"))
            {
                <td class="text-left">
                    @Html.ActionLink(
                         item.Customer.UserName,
                         "Details", "Customer",
                          new { id = item.Customer.CustomerID }, new { @class = "LabelNormal" })
                </td>

                <td class="text-left">
                    @Html.DisplayFor(modelItem => item.Customer.Name)
                </td>
            }
            
            <td class="text-right" title="@Html.Raw(item.DisplayDirectAmount)">
                @if (item.DirectAmount > 0)
                {
                    @Html.ActionLink(
                        @Html.Raw(string.Format("{0:#,0}", item.DirectAmount)).ToString(),
                        "Index",
                        new
                        {
                            dateFrom = item.AccountDate.ToShortDateString(),
                            dateTo = item.AccountDate.ToShortDateString(),
                            userName = item.Customer.UserName,
                            TransactionTypeID = 1
                        },
                        new
                        {
                            target = "_blank"
                        }
                    )
                }
                else
                {
                    @Html.Raw("-")
                }
            </td>
            

            <td class="text-right" title="@Html.Raw(item.RollupAmount)">
                @if (item.RollupAmount > 0)
                {
                    @Html.ActionLink(
                        @Html.Raw(string.Format("{0:#,0}", item.RollupAmount)).ToString(),
                        "Index",
                        new
                        {
                            dateFrom = item.AccountDate.ToShortDateString(),
                            dateTo = item.AccountDate.ToShortDateString(),
                            userName = item.Customer.UserName,
                            TransactionTypeID = 2
                        },
                        new
                        {
                            target = "_blank"
                        }
                    )
                }
                else
                {
                    @Html.Raw("-")
                }
            </td>

            <td class="text-right" title="@Html.Raw(item.DisplayPairingAmount)">
                @if (item.PairingAmount > 0)
                {
                    @Html.ActionLink(
                             @Html.Raw(string.Format("{0:#,0}", item.PairingAmount)).ToString(),
                        "Index",
                        new
                        {
                            dateFrom = item.AccountDate.ToShortDateString(),
                            dateTo = item.AccountDate.ToShortDateString(),
                            userName = item.Customer.UserName,
                            TransactionTypeID = 3
                        },
                        new
                        {
                            target = "_blank"
                        }
                    )
                }
                else
                {
                    @Html.Raw("-")
                }
            </td>

            <td class="text-right" title="@Html.Raw(item.DisplayOnlineAmount)">
                @if (item.OnlineAmount > 0)
                {
                    @Html.ActionLink(
                        @Html.Raw(string.Format("{0:#,0}", item.OnlineAmount)).ToString(),
                        "Index",
                        new
                        {
                            dateFrom = item.AccountDate.ToShortDateString(),
                            dateTo = item.AccountDate.ToShortDateString(),
                            userName = item.Customer.UserName,
                            TransactionTypeID = 4
                        },
                        new
                        {
                            target = "_blank"
                        }
                    )
                }
                else
                {
                    @Html.Raw("-")
                }
            </td>

            <td class="text-right" title="@Html.Raw(item.DisplayMatchingAmount)">
                @if (item.MatchingAmount > 0)
                {
                    @Html.ActionLink(
                        @Html.Raw(string.Format("{0:#,0}", item.MatchingAmount)).ToString(),
                        "Index",
                        new
                        {
                            dateFrom = item.AccountDate.ToShortDateString(),
                            dateTo = item.AccountDate.ToShortDateString(),
                            userName = item.Customer.UserName,
                            TransactionTypeID = 5
                        },
                        new
                        {
                            target = "_blank"
                        }
                    )
                }
                else
                {
                    @Html.Raw("-")
                }
            </td>

            <td class="text-right" title="@Html.Raw(item.DisplayUsedAmount)">
                @if (item.UsedAmount != 0)
                {
                    @Html.ActionLink(
                        @Html.Raw(string.Format("{0:#,0}", item.UsedAmount)).ToString(),
                        "Index",
                        new
                        {
                            dateFrom = item.AccountDate.ToShortDateString(),
                            dateTo = item.AccountDate.ToShortDateString(),
                            userName = item.Customer.UserName,
                            TransactionTypeID = -1
                        },
                        new
                        {
                            target = "_blank"
                        }
                    )
                }
                else
                {
                    @Html.Raw("-")
                }
            </td>

            <td class="text-right-bold" title="@Html.Raw(item.DisplayTotalAmount)">
                @Html.Raw(string.Format("{0:#,0}", item.TotalAmount))
            </td>

            <td class="text-right-bold-large"
                    title="@Html.Raw(item.AmountDetail)">
                @Html.Raw(string.Format("{0:#,0}", item.CumulativeAmount))
            </td>

            <td>
                @Html.ActionLink(@MyResources.LinkDetail, "Index",
                    new {
                        dateFrom = item.AccountDate.ToShortDateString(),
                        dateTo = item.AccountDate.ToShortDateString(),
                        userName = item.Customer.UserName,
                    })
            </td>
        </tr>
    }

    <tfoot>
        <tr>
            <th class="text-center">
                @if (!User.IsInRole("Admin"))
                {
                    @MyResources.LabelTotal
                }
            </th>

            @if (User.IsInRole("Admin"))
            {
                <th colspan="2">
                    @MyResources.LabelTotal
                </th>
            }

            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.DirectAmount)))
            </th>
            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.RollupAmount)))
            </th>
            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.PairingAmount)))
            </th>
            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.OnlineAmount)))
            </th>
            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.MatchingAmount)))
            </th>
            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.UsedAmount)))
            </th>
            <th class="text-right">
                @Html.Raw(string.Format("{0:#,0}", @Model.Sum(i => i.TotalAmount)))
            </th>
            <th class="text-right">
            </th>
            <th></th>
        </tr>
    </tfoot>

</table>
